clear;clc;

% =========================================================================
% 1. LOAD DATA: pe 
% =========================================================================
PAR_pe =[6 10:5:60];
RES_refi_pe = zeros(length(PAR_pe),1); RES_c_pe = zeros(length(PAR_pe),1); RES_ms_pe = zeros(length(PAR_pe),1);

for i=1:length(PAR_pe)
    disp(PAR_pe(i))
    filename =['FinE_pq7_q5_pe',num2str(PAR_pe(i)),'_e8.5_pf75_x75','.mat'];
    load(filename)
    
    RES_refi_pe(i) = mean(irf_refi_ypr(:,1))-mean(irf_refi_yp(:,1));
    RES_c_pe(i)    = mean(log(irf_c_ypr(:,1))-log(irf_c_yp(:,1)));
    RES_ms_pe(i)   = sum(irf_refi_ypr.*(qini~=0|eini==e))./sum(irf_refi_ypr);

end

save RES_pe PAR_pe RES_ms_pe RES_refi_pe RES_c_pe

% =========================================================================
% 2. LOAD DATA: e
% =========================================================================
PAR_e =[8.5 20:10:100];
RES_refi_e = zeros(length(PAR_e),1); RES_c_e = zeros(length(PAR_e),1); RES_ms_e = zeros(length(PAR_e),1);

for i=1:length(PAR_e)
    disp(PAR_e(i))
    filename =['FinE_pq7_q5_pe6_e',num2str(PAR_e(i)),'_pf75_x75','.mat'];
    load(filename)
    
    RES_refi_e(i) = mean(irf_refi_ypr(:,1))-mean(irf_refi_yp(:,1));
    RES_c_e(i)    = mean(log(irf_c_ypr(:,1))-log(irf_c_yp(:,1)));
    RES_ms_e(i)   = sum(irf_refi_ypr.*(qini~=0|eini==e))./sum(irf_refi_ypr);

end

save RES_e PAR_e RES_ms_e RES_refi_e RES_c_e


% =========================================================================
% 3. plot
% =========================================================================

clear;clc;
load RES_pe; load RES_e

subplot(2,3,1)
plot(PAR_pe,RES_ms_pe*100,'-s','LineWidth',1.5)
title("FinTech market share")
xlabel('p^e')
ylabel('Percent')
ylim([0 100])

subplot(2,3,2)
plot(PAR_pe,RES_refi_pe*100,'k-s','LineWidth',1.5)
title("Refi-propensity response")
xlabel('p^e')
ylabel('Change (pp)')
ylim([10 18])

subplot(2,3,3)
plot(PAR_pe,RES_c_pe*100,'r-s','LineWidth',1.5)
title("Consumption response")
xlabel('p^e')
ylabel('% Change')
ylim([1 2])

subplot(2,3,4)
plot(PAR_e/100,RES_ms_e*100,'-s','LineWidth',1.5)
title("FinTech market share")
xlabel('e')
ylabel('Percent')
ylim([0 100])

subplot(2,3,5)
plot(PAR_e/100,RES_refi_e*100,'k-s','LineWidth',1.5)
title("Refi-propensity response")
xlabel('e')
ylabel('Change (pp)')
ylim([10 18])

subplot(2,3,6)
plot(PAR_e/100,RES_c_e*100,'r-s','LineWidth',1.5)
title("Consumption response")
xlabel('e')
ylabel('% Change')
ylim([1 2])




